package com.wangyh.edit.prettyedit.login;

import com.wangyh.edit.prettyedit.StartApplication;
import com.wangyh.edit.prettyedit.constant.EditConstant;
import com.wangyh.edit.prettyedit.util.PreferencesUtils;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.util.Map;

/**
 * 构造登录界面，处理必要的初始化逻辑。
 * 登录事件需要到controler中查看
 * @author wangyh
 */
public class LoginView {

    private final Stage stage = new Stage();

    /**
     * 构造界面
     */
    public void showView(){
        FXMLLoader fxmlLoader = new FXMLLoader(StartApplication.class.getResource("/views/LoginView.fxml"));
        Scene scene = null;
        GridPane borderPane = null;
        try {
            borderPane = fxmlLoader.load();
            scene = new Scene(borderPane, EditConstant.loginWidth, EditConstant.loginHeight);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //设置窗体内容
        stage.setScene(scene);

        //设置窗体样式
        configStage();

        //初始化界面显示内容
        initUI(fxmlLoader);
    }

    /**
     * 设置窗体样式
     */
    private void configStage(){
        Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
        double width = screenRectangle.getWidth();
        double height = screenRectangle.getHeight();

        //标题
        stage.setTitle(EditConstant.appName);
        //设置窗体标题小图标和任务栏图标
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/img/Logo2.png")));
        //设置在屏幕中的显示位置
        stage.setX(width/2-EditConstant.loginWidth/2);
        stage.setY(height/2-EditConstant.loginHeight/2);
        stage.initStyle(StageStyle.DECORATED);
        stage.setResizable(false); //登录窗口的大小不允许改变
        stage.show();
//        stage.centerOnScreen();//窗口居中，必须再show之后调用才能起效
    }

    /**
     * 初始化界面显示内容
     * @param fxmlLoader
     */
    private void initUI(FXMLLoader fxmlLoader){
        Map<String, Object> namespace = fxmlLoader.getNamespace();

        //默认获取焦点
        CheckBox remPwd = (CheckBox) namespace.get("remPwd");
        remPwd.requestFocus();

        String remb = PreferencesUtils.getPreferences(EditConstant.storeConfig,EditConstant.configRemb,"");
        //记住账号密码
        if("1".equals(remb)){
            //获取登录人的密码
            String user = PreferencesUtils.getPreferences(EditConstant.storeConfig,EditConstant.configUser,"");
            String pwd = PreferencesUtils.getPreferences(EditConstant.storeConfig,EditConstant.configPwd,"");

            if(user!=null && user.trim().length()>0){
                TextField userText = (TextField) namespace.get("userText");
                //设置数值
                userText.setText(user);
            }
            if(pwd!=null && pwd.trim().length()>0){
                PasswordField pwdText = (PasswordField) namespace.get("pwdText");
                //设置数值
                pwdText.setText(pwd);
            }
            //记住账号密码
            remPwd.setSelected(true);
        }else{
            //不要记住账号密码
            remPwd.setSelected(false);
        }
    }


    //    @Override
//    public void start(Stage primaryStage){
//        GridPane pane=new GridPane();
//        pane.setAlignment(Pos.CENTER);
//        pane.setPadding(new Insets(11,12,13,14));
//        pane.setHgap(5);
//        pane.setVgap(5);
//
//        Label label1=new Label("Welcome");
//        label1.setFont(Font.font(20));
//        pane.add(label1,0,0);
//        pane.add(new Label("User Name:"),0,1);
//        pane.add(new TextField(),1,1);
//        pane.add(new Label("Password:"),0,2);
//        final PasswordField pb = new PasswordField();
//        pane.add(pb,1,2);
//        Button bt=new Button("Sign in");
//        pane.add(bt,1,3);
//        GridPane.setHalignment(bt, HPos.RIGHT);
//
//        if(pb.getText().equals("123")) {
//
//            bt.setOnAction(new EventHandler<ActionEvent>() {
//                public void handle(ActionEvent e) {
//                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
//                    alert.setContentText("登录成功");
//                    alert.setTitle("登录成功");
//                    alert.setHeaderText("");
//                    alert.show();
//                }
//            });
//        } else {
//            bt.setOnAction(new EventHandler<ActionEvent>() {
//                public void handle(ActionEvent e) {
//                    MainFrame mainFrame = new MainFrame("a","b");
//                    primaryStage.close();
////                    Alert alert = new Alert(Alert.AlertType.ERROR);
////                    alert.setContentText("登录失败");
////                    alert.setTitle("登录失败");
////                    alert.setHeaderText("");
////                    alert.show();
//                }
//            });
//        }
//
//        Scene scene=new Scene(pane,420,350);
//        primaryStage.setTitle("JavaFx Welcome");
//        primaryStage.setResizable(false); //登录窗口的大小不允许改变
//        primaryStage.setScene(scene);
//        primaryStage.show();
//    }
}
